package com.tencent.backstage.aspect;

import com.tencent.backstage.configuration.DomainName;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String sessionId =(String) httpServletRequest.getSession().getAttribute("sessionId");
        String servletPath = httpServletRequest.getServletPath();
        System.out.println(servletPath);
        if("/fireIndex/login".equals(servletPath)|"/fireIndex/adminLogin".equals(servletPath)) {
            log.info("this page for no Interceptor");
        }else{
            if (sessionId == null) {
                log.warn("login for Interceptor");
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/fireIndex/login");
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
